package com.hxkj.service.taglib.impl;

import com.alibaba.fastjson.JSONObject;
import com.hxkj.client.service.*;
import com.hxkj.domain.po.*;
import com.hxkj.service.taglib.AnakyzeFreeMakerShowService;
import com.hxkj.service.taglib.AnalyzeChannelService;
import com.hxkj.service.taglib.AnalyzeColumnService;
import com.hxkj.service.taglib.AnalyzeTagsService;
import com.hxkj.token.utils.TokenUtils;
import com.integral.mall.file.po.SysFile;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hxkj.domain.Constant.CmsConstants.*;

/**
 * @ClassName AnakyzeFreeMakerShowServiceImpl 可视化标签调用
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/8/16 14:28
 * @Version 1.0
 */
@Service
@Slf4j
public class AnakyzeFreeMakerShowServiceImpl implements AnakyzeFreeMakerShowService {

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private ResourcesServiceClient resourcesServiceClient;

    @Autowired
    private TemplteServiceClient templetServiceClient;

    /**
     * @author Mr.zheng
     * @Description anakyzeFreeMakerShow 可视化调用接口分流
     * @Date 18:29 2018/8/16
     **/
    @Override
    public JSONObject anakyzeFreeMakerShow(FreeMarker freeMarker) throws Exception {
        //用户id设置统一抽到这里设置一次
        freeMarker.setUserToken(((AuthUser) tokenUtils.getTokenUserInfo()).getUserId().toString());
        JSONObject jsonObject = new JSONObject();
        try {
            //Mr.zheng:1为频道模板调用
            if (freeMarker.getStatus() == CHANNEL) {
                jsonObject = freeMakerShowChannel(freeMarker);
                return jsonObject;
            }
            //Mr.zheng:2为文章详情模板调用
            if (freeMarker.getStatus() == ARTICLEDETAIL) {
                jsonObject = freeMakerShowArticle(freeMarker);
                return jsonObject;
            }
            //Mr.zheng:3为分类列表模板调用
            if (freeMarker.getStatus() == COLUMNLIST) {
                jsonObject = freeMakerShowColumn(freeMarker);
                return jsonObject;
            }
            //Mr.zheng:4为单页模板调用
            if (freeMarker.getStatus() == LEAFLET) {
                jsonObject = freeMakerShowLeaflet(freeMarker);
                return jsonObject;
            }
            //Mr.zheng:5为tags详情模板调用
            if (freeMarker.getStatus() == TAGSDEATIL) {
                jsonObject = freeMakerShowTag(freeMarker);
                return jsonObject;
            }
        } catch (CustomMessageException | CustomException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return jsonObject;
    }

    /**
     * @author Mr.zheng
     * @Description freeMakerShowTag 解析预览tag详情模板
     * @Date 18:00 2018/8/16
     **/
    @Autowired
    private AnalyzeTagsService analyzeTagsService;

    @Autowired
    private TagsServiceClient tagsServiceClient;

    private JSONObject freeMakerShowTag(FreeMarker freeMarker) throws Exception {
        try {

            JSONObject jsonObject = analyzeTagsService.visualizationTags(freeMarker);

            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }


    /**
     * @author Mr.zheng
     * @Description freeMakerShowColumn  解析预览分类列表模板
     * @Date 17:28 2018/8/16
     **/
    @Autowired
    private ColumnServiceClient columnServiceClient;

    @Autowired
    private AnalyzeColumnService analyzeColumnService;

    private JSONObject freeMakerShowColumn(FreeMarker freeMarker) throws Exception {
        return analyzeColumnService.visualizationColumn(freeMarker);
    }

    /**
     * @author Mr.zheng
     * @Description freeMakerShowArticle 解析预览文章详情模板
     * @Date 15:48 2018/8/16
     **/
    @Autowired
    private ArticleServiceClient articleServiceClient;

    @Autowired
    private AnalyzeArticleServiceClient analyzeArticleServiceClient;

    private JSONObject freeMakerShowArticle(FreeMarker freeMarker) throws Exception {
        Article article = new Article();
        article.setSiteId(freeMarker.getSiteId());
        article.setTempletId(freeMarker.getTempletId());
        ResultDataStruct<Article> resultDataStruct = articleServiceClient.getArticleByTempletId(article);
        if (resultDataStruct.getCode() != 200) {
            throw new CustomMessageException(resultDataStruct.getCode(), resultDataStruct.getMessage());
        }

        article = resultDataStruct.getData();
        freeMarker.setColumnId(article.getColumnId());
        freeMarker.setArticleId(article.getArticleId());
        JSONObject jsonObject = null;
        if (StringUtils.isBlank(freeMarker.getNote())) {
            analyzeArticleServiceClient.analyzeArticle(freeMarker);
        } else {
            ResultDataStruct<SysFile> result = analyzeArticleServiceClient.visualizationArticle(freeMarker);
            SysFile sysFile = result.getData();
            jsonObject = (JSONObject) JSONObject.toJSON(sysFile);
        }
        return jsonObject;
    }

    /**
     * @author Mr.zheng
     * @Description freeMakerShowChannel 解析预览频道模板
     * @Date 14:39 2018/8/16
     **/
    @Autowired
    private ChannelServiceClient channelServiceClient;

    @Autowired
    private AnalyzeChannelService analyzeChannelService;

    private JSONObject freeMakerShowChannel(FreeMarker freeMarker) throws Exception {

        return analyzeChannelService.visualizationChannel(freeMarker);
    }

    @Autowired
    private LeafletServiceClient leafletServiceClient;
    @Autowired
    private AnalyzeLeafletServiceClient analyzeLeafletServiceClient;

    //生成单页静页
    private JSONObject freeMakerShowLeaflet(FreeMarker freeMarker) throws Exception {
        Leaflet leaflet = new Leaflet();
        leaflet.setSiteId(freeMarker.getSiteId());
        leaflet.setTempletId(freeMarker.getTempletId());
        List<Leaflet> leaflets = leafletServiceClient.getLeafletList(leaflet).getData();
        if (leaflets == null || leaflets.size() == 0) {
            throw new CustomMessageException(401, "没有获取到单页数据!");
        }

        freeMarker.setChannelId("0");
        freeMarker.setAliasName("leaflet");
        freeMarker.setArticleId(leaflets.get(0).getLeafletId());
        freeMarker.setStatus(LEAFLET);
        ResultDataStruct resultDataStruct = analyzeLeafletServiceClient.analyzeLeaflet(freeMarker);
        if (resultDataStruct.getCode() != 200) {
            throw new CustomMessageException(resultDataStruct.getCode(), "单页模板错误！错误信息：" + resultDataStruct.getMessage());
        }
        SysFile data = (SysFile) resultDataStruct.getData();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("filePath", data.getFilePath());
        jsonObject.put("fileName", data.getFileName());
        jsonObject.put("fileSize", data.getFileSize());
        jsonObject.put("fileType", data.getFileType());
        return jsonObject;
    }


    @Override
    public void modifyMd5(VisualBean visualBean) throws Exception {
        if (visualBean.getSiteId() == null) {
            throw new CustomMessageException(402, "站点id为空!");
        }
        ResultMessageStruct resultMessageStruct;
        //修改模板文件MD5值
        try {
            Templet templet = new Templet();
            templet.setSiteId(visualBean.getSiteId());
            resultMessageStruct = templetServiceClient.modifyTempletMd5(templet);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomMessageException(resultMessageStruct.getCode(), resultMessageStruct.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        //修改资源文件MD5值
        try {
            Resources resources = new Resources();
            resources.setSiteId(visualBean.getSiteId());
            resultMessageStruct = resourcesServiceClient.modifyResourcesMd5(resources);
            if (resultMessageStruct.getCode() != 200) {
                throw new CustomMessageException(resultMessageStruct.getCode(), resultMessageStruct.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
